package com.wc.AlgoOJ_train.train2_DP训练.c4.DPE007;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/13 2:42
 * @description
 */
public class Main2 {
    static int maxN = (int) 1e5 + 7;
    //邻接表，key节点，value 能到达的地方
    static HashMap<Integer, ArrayList<Integer>> grid = new HashMap<>();

    // 每一个位置的入度
    static int[] rds = new int[maxN];
    static int n, m;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int[] dp = new int[maxN];

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            grid.put(i, new ArrayList<>());
        }
        for (int i = 1; i <= m; i++) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            rds[v]++;
            grid.get(u).add(v);
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            if (rds[i] == 0) {
                ans = Math.max(dfs(i), ans);
            }
        }
        out.println(ans);
        out.flush();
    }

    static int dfs(int u) {
        if (dp[u] != 0) {
            return dp[u];
        }
        int maxLen = 0;
        ArrayList<Integer> vs = grid.get(u);
        for (int v : vs) {
            maxLen = Math.max(maxLen, dfs(v) + 1);
        }
        dp[u] = maxLen;
        return dp[u];
    }

    static class FastReader {
        StringTokenizer st;
        BufferedReader br;

        FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        String nextLine() {
            String s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}
